// index.js
// 获取应用实例
const app = getApp();
Page({
  onShareAppMessage() {
    return {
      title: 'swiper',
      path: 'page/index/image/swiper'
    }
  },
  data: {
    // pageTitle:"智慧医疗",
    navH:60,
    swipers: [],
    indicatorDots: true,//是否显示小点点
    vertical: false,
    autoplay: true,
    interval: 2000,
    duration: 500,
    motto: 'Hello World123',
    active:0,
    userInfo: {},
    dataItems:[
    ],
    hasUserInfo: false,
    canIUseGetUserProfile: false,
    noticeTitle:'',
    noticeId:'',
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onShow(){
    // console.log(111);
  },
  onLoad() { 
    wx.getStorage({
      key: 'token',
      success (res) {
        app.globalData.token = res.data;
      }
    })
    wx.getStorage({
      key: 'userInfo',
      success (res) {
        app.globalData.userInfo = res.data;
      }
    })
    wx.getUserInfo({
      withCredentials:false,
      success: function(res) {
        app.globalData.userImg =  res.userInfo.avatarUrl;
      }
    })
    this.getModules();
    this.getSliders();
    this.getBoard();
  },
  // 获取检查模块
  getModules(){
    let _this = this;
    wx.request({
      url: app.globalData.baseServePath+'/wxapi/category/list', //仅为示例，并非真实的接口地址
      data: {
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        if(res.statusCode == 200){
          let arr = [];
          for(let i = 0; i < res.data.data.length;i ++ ){
            let item = res.data.data[i];
            arr.push({id:item.id,icon:item.categoryImg,label:item.categoryName});
          }
          _this.setData({
            dataItems:arr
          })
        }
      } 
    })
  },
  getSliders(){
    var _this = this;
    wx.request({
      url: app.globalData.baseServePath+'/wxapi/banner/list', //仅为示例，并非真实的接口地址
      data: {
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        if(res.statusCode == 200 && res.data.data){
          var swipersArray = [];
          var data = res.data.data;
          for (let i = 0; i < data.length; i++) {
            swipersArray.push(data[i].bannerImg);
          }
          _this.setData({swipers:swipersArray});
        }
      }
    })
  },
  onChangeTab(val){
    this.setData({active:val.detail});
  },
  //查看检查数据
  goDataView(e){
    wx.navigateTo({
      url: '/pages/dataView/dataView?id='+e.currentTarget.id,
    })
  },
  getLocation(){
       // 获取当前的经纬度坐标
    return new Promise((resolve,reject)=>{
      wx.getLocation({
        type:'gcj02',
        altitude:true,
        isHighAccuracy:true,
        success(res) {
          // 纬度
          let latitude = res.latitude
          // 经度
          let longitude = res.longitude
          resolve([longitude,latitude]);
        },
        fail(res){
          reject(res);
        }
      })
    })
  },
  alarm(){
    if(!app.globalData.token){
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }else{
      this.takeAlarm();
    }
  },
  //报警
  takeAlarm(){
    debugger
    this.getLocation().then((res)=>{
      let key = '852883d4711b669de36dbc14b01b8b62';
      var lng = res[0];
      var lat = res[1];
      // 请求高德地图逆地址解析接口
      wx.request({
          url: `https://restapi.amap.com/v3/geocode/regeo?output=json&location=${lng},${lat}&key=${key}&radius=1000&extensions=all`,
          success(res) {
              if(res.data.info == 'OK'){
                wx.request({
                  url: app.globalData.baseServePath+'/valid/wxapi/sos',
                  method:'POST',
                  data:{
                    'sosName':app.globalData.userInfo.userName,
                    'sosPhone':app.globalData.userInfo.phonenumber,
                    'sosAddress':res.data.regeocode.formatted_address,
                    'locationX':lat.toString(),
                    'locationY':lng.toString(),
                    'status':'0'
                  },
                  header:{
                    Authorization:'Bearer '+app.globalData.token
                  },
                  success (res) {
                    if(!!res.statusCode && res.statusCode == 200){
                      if(res.data.code == 200){
                        wx.showToast({
                          title: '报警成功',
                          icon: 'success',
                          duration: 2000
                        })
                      }else{
                          console.log(res.data.msg)
                      }
                    }else{
                      console.log('后台出错了')
                    }                    
                  }
                })
              }
              
          }
      })
    })
    
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log('用户信息：'+res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getBoard(){
    let _this = this;
    wx.request({
      url: app.globalData.baseServePath + '/wxapi/notice/latest',
      success(res){
        if(res.data && res.data.code == 200){
          _this.setData({noticeTitle:res.data.data.noticeTitle,noticeId:res.data.data.noticeId})
        }
      }
    })
  },
  boardView(){
    wx.navigateTo({
      url: '/pages/boardView/boardView?id='+this.data.noticeId,
    })
  }
})
